﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.User
{
    public class AddLoginHandler : IRequestHandler<AddLoginCommand, Result>
    {

        private readonly IBaseRepository<Users> usersRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="usersRepository"></param>
        public AddLoginHandler(IBaseRepository<Users> usersRepository)
        {
            this.usersRepository = usersRepository;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(AddLoginCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            var list = await (await usersRepository.GetAll()).FirstAsync(d => d.UserName == request.UserName && d.UserPwd == request.UserPwd);
            if (list != null)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Data = list;
                result.Msg = "用户登录成功";
            }
            return result;
        }
    }
}
